/*
 * Copyright 2015 Hongxin Telecommunication Technologies Co, Ltd.,
 * Wuhan, Hubei, China. All rights reserved.
 */
package com.hxrd;

import com.hxrd.data.Device;
import com.hxrd.interaction.DBManage;
import com.hxrd.interaction.InterActive;
import com.hxrd.job.DevicePollManager;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static com.hxrd.interaction.InterActive.getTrace;

/**
 *
 * @author chenwei Created on 2016-12-22, 18:12:42
 */
public class Luncher
{
	private static Log log = LogFactory.getLog(Luncher.class);
	private static int interval = 4;//min
	private Map<String, Device> allDevice = new HashMap<String, Device>();
	public static int sessionTimeout = 5;
	public static int commandTimeout = 2;
	public static int commandSleep = 2;

	public Luncher()
	{
	}

	public static void main(String[] args) 
	{
		Luncher luh = new Luncher();
		ReadProperty();
		luh.initDevice(new DBManage().getDB());
		log.info("sessionTimeout=" + sessionTimeout);
		log.info("commandTimeout=" + commandTimeout);
		log.info("commandSleep=" + commandSleep);
		luh.startPoll();
		new InterActive().InterAction(luh);
		log.info("start all poll job");
	}

	private void startPoll()
	{
		for(Map.Entry<String, Device> entry : allDevice.entrySet())
		{
			Device dev = entry.getValue();
			DevicePollManager.addAcJob(dev, interval);
		}
	}

	public void addDevice(Device dev)
	{
		allDevice.put(dev.getIp(), dev);
		DevicePollManager.addAcJob(dev, interval);
	}

	public void deleteDevice(String ip)
	{
		allDevice.remove(ip);
		DevicePollManager.removeACJob(ip);
	}

	public void clearAllDevice()
	{
		allDevice.clear();
		DevicePollManager.shutdownJobs();
	}

	public void initDevice(ArrayList<Device> ad)
	{
		for(Device e : ad)
		{
			allDevice.put(e.getIp(), e);
		}
	}

	public static void ReadProperty()
	{
		Properties pro = new Properties();//属性集合对象
		FileInputStream path = null;
		//获取项目中文件的路径
		try
		{
			path = new FileInputStream("lib/argument.properties");
		}
		catch(FileNotFoundException e)
		{
			System.out.println("属性文件不存在，将自动按默认方式运行！");
			log.info(getTrace(e));
			return;
		}
		//获取路径并转换成流
		try
		{
			pro.load(path);
			//判断属性值是否为数字
			if(!IntegerDefine(pro.getProperty("sessionTimeout")))
			{
				log.info("sessionTimeout不为整数");
				return;
			}
			if(!IntegerDefine(pro.getProperty("commandTimeout")))
			{
				log.info("commandTimeout不为整数");
				return;
			}
			if(!IntegerDefine(pro.getProperty("commandSleep")))
			{
				log.info("commandSleep不为整数");
				return;
			}
			// 判断属性值是否在范围内
			if(Integer.parseInt(pro.getProperty("sessionTimeout")) < 1 || Integer.
					parseInt(pro.getProperty("sessionTimeout")) > 100)
			{
				log.info("sessionTimeout=" + Integer.parseInt(pro.
						getProperty("sessionTimeout")) + ",超出范围！");
				return;
			}
			if(Integer.parseInt(pro.getProperty("commandTimeout")) < 1 || Integer.
					parseInt(pro.getProperty("commandTimeout")) > 50)
			{
				log.info("commandTimeout=" + Integer.parseInt(pro.
						getProperty("commandTimeout")) + ",超出范围！");
				return;
			}
			if(Integer.parseInt(pro.getProperty("commandSleep")) < 1 || Integer.
					parseInt(pro.getProperty("commandSleep")) > 50)
			{
				log.info("commandSleep=" + Integer.parseInt(pro.
						getProperty("commandSleep")) + ",超出范围！");
				return;
			}
			sessionTimeout = Integer.parseInt(pro.
					getProperty("sessionTimeout"));
			commandTimeout = Integer.parseInt(pro.
					getProperty("commandTimeout"));
			commandSleep = Integer.parseInt(pro.
					getProperty("commandSleep"));
		}
		catch(IOException e)
		{
			log.info(getTrace(e));
		}
	}

	public static boolean IntegerDefine(String s)
	{
		for(int i = 0; i < s.length(); i++)
		{
			if(s.charAt(i) < '0' || s.charAt(i) > '9')
			{
				return false;
			}
		}
		return true;
	}

}
